#pragma once
#include "../gm_api/gm_global.h"
#include "gm_handle.h"


class gm_sc_key : public gm_handle
{
public:

	gm_sc_key(ULONG ulid, ULONG ulContId, ULONG ulAlgid, ULONG ulagre = 0);
	gm_sc_key(ULONG ulid, ULONG ulAlgid);
	~gm_sc_key(void);

public:
	ULONG key_id() const { return _ulid; }
	void key_id(ULONG val) { _ulid = val; }

	ULONG Algid() const { return _ulAlgid; }
	void Algid(ULONG val) { _ulAlgid = val; }

	ULONG get_cont_id() { return _ulContId; }

	ULONG IsAgre() const { return _ulagre; }
	void IsAgre(ULONG val) { _ulagre = val; }

	ULONG get_block_size();

	void set_padding_type(ULONG PaddingType) { _ulPaddingType = PaddingType; } ;

	ULONG get_padding_type() { return _ulPaddingType;}

	static ULONG get_block_size(ULONG algId);

	static ULONG get_key_size(ULONG algId);

	static ULONG get_dev_alg_id(ULONG algId);

	BYTE * init_mem_stream(int length);

	void   release_mem_stream();

	gm_stream_mgr * get_stream_mgr() { return &_stream;}

	ULONG get_decrypt_data_len();

	ULONG get_encrypt_data_len();
	
	ULONG get_padding_result_len(ULONG data_len);

	ULONG get_block_align_result_len( ULONG data_len );

	ULONG check_alg_data_len(ULONG data_len);

	ULONG check_iv_len(ULONG ulIVLen);

	int  set_iv(unsigned char * iv, int iv_len);

	unsigned char * get_iv(int * iv_len);

	void set_first_package(int bFirst);

	int  get_first_package();

	int  set_sessionkey(BYTE *key, ULONG kenlen);

	int  get_sessionkey(BYTE *key, ULONG *kenlen);

	BYTE*  get_sessionkey(){return _key;}

private:
	
	ULONG	_ulid;
	ULONG	_ulAlgid;
	ULONG	_ulContId;
	ULONG   _ulagre;
	ULONG   _ulPaddingType;

	unsigned char _iv[32];
	int		 _iv_len;

	int		 _is_first_package;

	BYTE  _key[32];
	ULONG _kenlen;
	
	gm_stream_mgr _stream;
};
